import { getCurrentInstance, watch, ref } from 'vue'

// 使用函数式组件
export function useFunctional() {
  const { proxy } = getCurrentInstance()

  function message(msg, direction) {
    proxy.$message({ msg, direction })
  }

  function dialog(config) {
    proxy.$dialog({ config })
  }

  return {
    message,
    dialog,
  }
}

// 获取验证码
export function useCaptcha() {
  // 验证码数据
  let captchaInfo = ref({
    msgShow: true,
    msgCount: '',
    timer: null,
  })

  // 开始倒计时
  function startCountdown() {
    const TIME_COUNT = 60
    if (!captchaInfo.value.timer) {
      captchaInfo.value.msgCount = TIME_COUNT
      captchaInfo.value.msgShow = false
      captchaInfo.value.timer = setInterval(() => {
        if (captchaInfo.value.msgCount > 0 && captchaInfo.value.msgCount <= TIME_COUNT) {
          captchaInfo.value.msgCount--
        } else {
          captchaInfo.value.msgShow = true
          clearInterval(captchaInfo.value.timer)
          captchaInfo.value.timer = null
        }
      }, 1000)
    }
  }

  return {
    captchaInfo,
    startCountdown,
  }
}
